home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8099 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: newshost.cyberramp.net!news
  2. From: sinan@cyberramp.net (John Noland)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How do I modify a character string that's declared as an array of char?
  5. Date: 1 Mar 1996 15:42:25 GMT
  6. Organization: Prose Software
  7. Distribution: world
  8. Message-ID: <4h75t1$q7s@newshost.cyberramp.net>
  9. References: <4gj2nl$840@mirzam.usc.edu> <4gqo63$2pr@newshost.cyberramp.net> <TANMOY.96Feb25203754@qcd.lanl.gov>
  10. NNTP-Posting-Host: ramp3-28.cyberramp.net
  11. X-Newsreader: WinVN 0.99.5
  12.  
  13. In article <TANMOY.96Feb25203754@qcd.lanl.gov>, tanmoy@qcd.lanl.gov says...
  14. >
  15. >In article <4gqo63$2pr@newshost.cyberramp.net>
  16. >sinan@cyberramp.net (John Noland) writes:
  17. >
  18. >JN: >   char mydata[50];
  19. >JN: >
  20. >JN: >   strcpy(mydata,"test string");
  21. >JN: >   myfunction(&mydata);
  22. >JN:                ^ 
  23. >JN: Dereferencing mydata isn't necessary. mydata is a pointer to the first 
  24. >JN: character of your array, so what you're really doing is passing a pointer
  25. >JN: to that pointer.
  26. >
  27. >Wrong. mydata is an array that usually decays to a pointer to the
  28. >first character. One place where it does not decay is when it is the
  29. >operand of &. &mydata is a pointer to the entire array. Read the FAQ
  30. >for more details.
  31. >
  32.  
  33. ANSI treats &mydata as a pointer to the entire array. Some compilers treat 
  34. &mydata as a pointer to the first element. Some compilers disallow &mydata
  35. constructs altogether. Hence, this is not a portable construct.:-) Use it if 
  36. you like, me, I'll pretend it's not legal. Preferring instead:
  37.  
  38. char (*p)(50);
  39.  
  40. p = (char (*)[50]) mydata;
  41.  
  42.  
  43. Just my opinion. Your code doesn't have to be portable.
  44.  
  45. -John
  46.  
  47.